package com.dm0858.bianmin.ui.activity;


import net.tsz.afinal.http.AjaxParams;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

public class HttpRequest extends AjaxParams {

	public static String basePath = "http://bmshfw.net/phpapi/info.php?func=";


	public String funcName;


	public String getFuncName() {

		return basePath + funcName;
	}

	public void setFuncName(String funcName) {
		this.funcName = funcName;
	}

	public void genParams() {
		try {
			HashMap<String, Object> map = HttpRequest.objToHash(this);

			Iterator<Entry<String, Object>> it = map.entrySet().iterator();

			while (it.hasNext()) {

				Entry keyEmtry = it.next();

				Object keyObj = (Object) keyEmtry.getKey();

				String key = keyObj.toString();

				Object valueObj = null == (Object) map.get(key) ? ""
						: (Object) map.get(key);

				String value = valueObj.toString();

				if (!key.equals("funcName") && !key.equals("ENCODING")
						&& !key.equals("fileParams")
						&& !key.equals("urlParams")) {

					this.put(key, value);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/*
	 * ! 实体类与HashMap相互转换
	 */
	public static HashMap<String, Object> objToHash(Object obj)
			throws IllegalArgumentException, IllegalAccessException {

		HashMap<String, Object> hashMap = new HashMap<String, Object>();

		@SuppressWarnings("rawtypes")
        Class clazz = obj.getClass();

		@SuppressWarnings("rawtypes")
        List<Class> clazzs = new ArrayList<Class>();

		do {
			clazzs.add(clazz);
			clazz = clazz.getSuperclass();
		} while (!clazz.equals(Object.class));

		for (@SuppressWarnings("rawtypes")
                Class iClazz : clazzs) {
			Field[] fields = iClazz.getDeclaredFields();
			for (Field field : fields) {
				Object objVal = null;
				field.setAccessible(true);
				objVal = field.get(obj);
				hashMap.put(field.getName(), objVal);
			}
		}

		return hashMap;
	}
}
